package com.shire.web.console.controller.kf;

import com.shire.web.console.common.Response;
import com.shire.web.console.dao.account.UserAccountDao;
import com.shire.web.console.model.account.UserAccount;
import com.shire.web.console.model.account.UserAccountExample;
import com.shire.web.console.model.account.WxAccount;
import com.shire.web.console.model.account.WxAccountExample;
import com.shire.web.console.model.user.SystemUser;
import com.shire.web.console.service.account.WxAccountService;
import com.shire.web.console.service.user.SystemUserService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/wx")
public class WxAccountController {

    @Resource
    private WxAccountService accountService;
    @Resource
    private SystemUserService userService;
    @Resource
    private UserAccountDao userAccountDao;

    @GetMapping("/manage")
    public ModelAndView manageKf(HttpSession session){
        SystemUser curUser = (SystemUser) session.getAttribute("user");
        if (curUser==null)
            return new ModelAndView("redirect:/login");
        ModelAndView ma = new ModelAndView("account/list");
        ma.addObject("users",userService.getKfUsers(curUser.getId()));
        return ma;
    }

    @GetMapping("/listForUser")
    @ResponseBody
    public Response listForUser(@RequestParam Integer userId, HttpSession session){
        Response response = new Response();
        try{
            if (session.getAttribute("user")==null){
                response.error("请登录");
               return response;
            }

            UserAccountExample userAccountExample = new UserAccountExample();
            userAccountExample.createCriteria().andUser_idEqualTo(userId);
            List<UserAccount> list = userAccountDao.selectByExample(userAccountExample);

            List<String> idList = new ArrayList<>();
            for (UserAccount it : list){
                idList.add(it.getWx_account());
            }

            WxAccountExample example = new WxAccountExample();
            example.createCriteria().andWxIdIn(idList);
            response.setData(accountService.selectByExample(example));
            response.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error("发生错误："+e.getMessage());
        }
        return response;
    }

    @GetMapping("/list")
    @ResponseBody
    public Response list(HttpSession session){
        Response response = new Response();
        try{
            if (session.getAttribute("user")==null){
                response.error("请登录");
                return response;
            }
            SystemUser curUser = (SystemUser) session.getAttribute("user");
            WxAccountExample example = new WxAccountExample();
            example.createCriteria().andOpUserIdEqualTo(curUser.getId());
            response.setData(accountService.selectByExample(example));
            response.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error("发生错误："+e.getMessage());
        }
        return response;
    }

    @GetMapping("/detail")
    public ModelAndView detail(Integer userId,HttpSession session){
        SystemUser curuser = (SystemUser) session.getAttribute("user");
        if (curuser==null)
            return new ModelAndView("redirect:/login");
        ModelAndView ma = new ModelAndView("account/detail");
        WxAccount user = accountService.selectById(userId);
        ma.addObject("users",userService.getKfUsers(curuser.getId()));
        ma.addObject("account",user);
        return ma;
    }

    @PostMapping("/save")
    @ResponseBody
    public Response save(@RequestBody WxAccount account, HttpSession session){
        Response response = new Response();
        try{
            if (session.getAttribute("user")==null){
                response.error("请登录");
                return response;
            }
            accountService.save(account);
            UserAccountExample example = new UserAccountExample();
            example.createCriteria().andWx_accountEqualTo(account.getWxId());
            UserAccount userAccount = userAccountDao.selectByExample(example).get(0);
            userAccount.setUser_id(account.getCsId());
            userAccountDao.updateByPrimaryKey(userAccount);
        }catch (Exception e){
            e.printStackTrace();
            response.error("发生错误："+e.getMessage());
        }
        return response;
    }

    @PostMapping("/addAccount")
    @ResponseBody
    public Response addCount(@RequestBody WxAccount account, HttpSession session){
        Response response = new Response();
        try{
            SystemUser curUser = (SystemUser) session.getAttribute("user");
            if (curUser==null){
                response.error("请登录");
                return response;
            }
            account.setAlias(account.getWxId());
            account.setInitStatus(1);
            accountService.save(account);
            UserAccount userAccount = new UserAccount();
            userAccount.setUser_id(account.getCsId());
            userAccount.setWx_account(account.getAlias());
            userAccountDao.insert(userAccount);
        }catch (Exception e){
            e.printStackTrace();
            response.error("发生错误："+e.getMessage());
        }
        return response;
    }

    @PostMapping("/unbind")
    @ResponseBody
    public Response unbind(@RequestBody WxAccount account, HttpSession session){
        Response response = new Response();
        try{
            if (session.getAttribute("user")==null){
                response.error("请登录");
                return response;
            }
            WxAccount wxAccount = new WxAccount();
            wxAccount.setId(account.getId());
            wxAccount.setCsId(0);
            accountService.save(wxAccount);
        }catch (Exception e){
            e.printStackTrace();
            response.error("发生错误："+e.getMessage());
        }
        return response;
    }
}
